/**
 * @file yuv2.c
 * @author your name (you@domain.com)
 * @brief 将一个yuv444像素数据文件，分离为y 、u 、v 文件
 * @version 0.1
 * @date 2021-04-27
 * 
 * @copyright Copyright (c) 2021
 * 
 */
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

int simplest_yuv444p_split(char * url, int w, int h, int num);

int main(void)
{
    simplest_yuv444p_split("../resource/lena_256x256_yuv444p.yuv", 256, 256, 1);
    return 0;
}

int simplest_yuv444p_split(char * url, int w, int h, int num)
{
    FILE * fp = fopen(url, "rb+");
    if(fp == NULL) {
        printf("fopen failed[%s] \n", strerror(errno));
        return -1;
    }
    FILE *fp1 = fopen("output_444_y.y", "wb+");
    FILE *fp2 = fopen("output_444_u.u", "wb+");
    FILE *fp3 = fopen("output_444_v.v", "wb+");

    uint8_t * pic = (uint8_t *)malloc(w * h * 3);

    for (int i = 0; i < num; i++)
    {
           fread(pic, 1, w * h * 3, fp) ;

           fwrite(pic, 1, w *h, fp1);

           fwrite(pic + w *h, 1, w *h, fp2);

           fwrite(pic + w *h *2, 1, w *h, fp3);
    }

    free(pic);
    fclose(fp);
    fclose(fp1);
    fclose(fp2);
    fclose(fp3);   

    return 0;
}